package com.zb.knowledge.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zb.entity.ZjKnowledgeTypeVideo;
import com.zb.entity.ZjVideo;
import com.zb.knowledge.service.ContVideoService;
import com.zb.service.ZjKnowledgeTypeVideoService;
import com.zb.service.ZjVideoContentService;
import com.zb.service.ZjVideoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author WEN
 * @Date 2023/8/14 0014
 **/
@Service
public class ContVideoServiceImpl implements ContVideoService {
    @Resource
    private ZjVideoService videoService;
    @Resource
    private ZjVideoContentService videoContentService;
    @Resource
    private ZjKnowledgeTypeVideoService knowledgeTypeVideoService;

    // 批量删除视频
    @Override
    @Transactional
    public void deleteByIds(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        knowledgeTypeVideoService.deleteBatchByVideoId(idList);
        videoService.removeByIds(idList);
        videoContentService.deleteByVideoId(idList);
    }

    // 知识库分类ID
    @Override
    public Object getVideo(Long knowledgeTypeId) {
        QueryWrapper<ZjKnowledgeTypeVideo> typeVideoQueryWrapper = new QueryWrapper<>();
        typeVideoQueryWrapper.select("knowledge_type_id", "video_id");
        typeVideoQueryWrapper.eq("knowledge_type_id", knowledgeTypeId);
        List<ZjKnowledgeTypeVideo> typeVideoList = knowledgeTypeVideoService.list(typeVideoQueryWrapper);
        if (CollectionUtil.isEmpty(typeVideoList)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjVideo> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.select("id", "name", "image");
        videoQueryWrapper.in("id", typeVideoList.stream().map(ZjKnowledgeTypeVideo::getVideoId).toList());
        List<ZjVideo> videoList = videoService.list(videoQueryWrapper);
        return videoList.stream().map(video -> {
            Map<String, Object> map = new HashMap<>();
            map.put("id", video.getId());
            map.put("name", video.getName());
            map.put("image", video.getImage());
            return map;
        }).toList();
    }
}
